// import {
//   GetProductInfo
// } from '@/api/onlineMan'

const selectProduct = {
  state: {
    // 活动支付方式
    payWay: '0', // 默认0 活动 支付方式 0 积分+金额 1 积分 2 金额
    sort: 'def', // 默认def def 系统默认排序 zdy 自定义排序

    /* 选中的商品 ids集合 */
    selectedProductIds: [],

    /* 选中的商品集合 */
    selectedProductList: [],

    /* 已存在的商品集合 */
    existencedProductList: [],

    /* 商品下的规格集合 */
    specList: [],

    /* 商品下的规格类型集合 */
    skusList: [],

    /* 批量设置 */
    batchList: [{
      key: 'usePointLimit',
      name: '活动积分',
      payWay: '2',
      reg: /^([1-9]\d*|[0]{1,1})$/ // 含零的正整数正则
    }, {
      key: 'price',
      name: '活动价格',
      payWay: '1',
      reg: /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/ // 含零的正整数正则 小数两位
    }]
  },
  mutations: {
    /* 设置活动支付方式 */
    SET_PAY_WAY: (state, data) => {
      state.payWay = data
    },

    /* 设置活动排序方式 */
    SET_ACTIVITYL_SORT: (state, data) => {
      state.sort = data
    },

    /* 设置选中商品 ids */
    SELECTED_PRODUCT_LIST: (state, data) => {
      const indexIds = state.selectedProductIds
      data.forEach(e => {
        if (indexIds.indexOf(e.productId) <= -1) {
          // 不存在id push进入
          e.sort = e.sort ? e.sort : ''
          state.selectedProductIds.push(e.productId)
          state.selectedProductList.push(e)
        } else {
          state.existencedProductList.push(e.name)
        }
      })
    },

    /* 覆盖选中商品 */
    ASSIGN_SELECTED_PRODUCT_LIST: (state, data) => {
      state.selectedProductList = data
      state.selectedProductIds = data.map(e => e.productId)
      state.existencedProductList = data.map(e => e.name)
    },

    /* 设置商品下的规格 */
    SET_SPEC_LIST: (state, data) => {
      state.specList = data || []
    },

    /* 设置商品下的规格 */
    SET_SPEC_TYPE_LIST: (state, data) => {
      state.skusList = data || []
    },

    /* 清空 */
    CLEAR_PRODUCT: (state, data) => {
      state.selectedProductIds = []
      state.selectedProductList = []
      state.existencedProductList = []
      state.specList = []
      state.skusList = []
      state.payWay = '0'
      state.sort = 'def'
    }

  },
  actions: {
    setActivityPayWay: ({
      commit
    }, data = []) => {
      commit('SET_PAY_WAY', data)
    },
    setActivitySort: ({
      commit
    }, data = []) => {
      commit('SET_ACTIVITYL_SORT', data)
    },
    setSelectedProductList: ({
      commit
    }, data = []) => {
      commit('SELECTED_PRODUCT_LIST', data)
    },
    assignSelectedProductList: ({
      commit
    }, data = []) => {
      commit('ASSIGN_SELECTED_PRODUCT_LIST', data)
    },
    clearSelectProduct: ({
      commit
    }, data) => {
      commit('CLEAR_PRODUCT', data)
    },
    setProductSpecList: ({
      commit
    }, data) => {
      commit('SET_SPEC_LIST', data.stocks)
      commit('SET_SPEC_TYPE_LIST', data.skus)
    }
  }
}

export default selectProduct
